<?php

/**
 * Created by PhpStorm.
 * User: wen
 * Date: 2017/5/3
 * Time: 13:16
 */
class C_add_invoice
{
    private $resource;

    private $call_items;

    private $back_items;

    private $resource_id;

    private $payload;

    /*
         * 开具类型 1个人 2企业
         */
    const RECEIPT_VAL_ONE = 1; //个人
    const RECEIPT_VAL_TWO = 2; //企业
    /*
     * 发票类型
     * 1增值税普通发票
     * 2增值税专用发票
     */
    const INVOICE_VAL_ONE = 1; //
    const INVOICE_VAL_TWO = 2; //
    public function __construct()
    {
        $this->resource = 'C_Invoice';

        $this->call_items = array(
            "t" => time(),
            "k" => "",
            'uid' => '',
            'v' => API_VERSION,
            "enterpriseId" => "",
            "channelId" => "",
            "userType" => ""
        );

        $this->back_items = array();
    }

    public function set_call_items($obj = array())
    {
        foreach ($this->call_items as $key => $val) {
            if (isset($obj[$key])) {
                $this->call_items[$key] = $obj[$key];
            }
        }
//        receipt_type=1&price=55.00&invoice_type=1&v=6
        if ($obj['receipt_val'] == self::RECEIPT_VAL_ONE){
            $this->payload = base64_encode('receipt_type='.$obj['receipt_val'].'&price='.$obj['invoice_price'].'&invoice_type='.$obj['invoice_val'].'&v='.$this->call_items['v']);
        }elseif ($obj['receipt_val'] == self::RECEIPT_VAL_TWO && $obj['invoice_val'] == self::INVOICE_VAL_ONE){
            $this->payload = base64_encode('title='.$obj['invoice_rise'].'&receipt_type='.$obj['receipt_val'].'&price='.$obj['invoice_price'].'&invoice_type='.$obj['invoice_val'].'&v='.$this->call_items['v']);
        }elseif ($obj['receipt_val'] == self::RECEIPT_VAL_TWO && $obj['invoice_val'] == self::INVOICE_VAL_TWO){
            $this->payload = base64_encode('bank_account='.$obj['bank_account'].'&deposit_bank='.$obj['account_bank'].'&telephone='.$obj['telephone'].'&current_address='.$obj['address'].'&social_credit_code='.$obj['credit_code'].'&account_name='.$obj['account_name'].'&title='.$obj['invoice_rise'].'&receipt_type='.$obj['receipt_val'].'&price='.$obj['invoice_price'].'&invoice_type='.$obj['invoice_val'].'&v='.$this->call_items['v']);
        }
        $this->call_items['k'] = md5($obj['password'].$this->call_items['t'] . $this->payload);
        return $this->_get_url();
    }

    public function set_back_items($data = array())
    {
    }

    public function set_resource_id($id)
    {
        $this->resource_id = $id;
        return $this->_get_url();
    }

    public function get_call_items()
    {
        return 'payload=' . $this->payload;
    }

    public function get_back_items()
    {
        return $this->back_items;
    }

    public function get_resource_id()
    {
        return $this->resource_id;
    }

    private function _get_url()
    {
        $query_arr = array();
        foreach ($this->call_items as $key => $val) {
            $query_arr[] = $key . '=' . $val;
        }
        if ($this->resource_id) {
            return $this->resource . '/' . $this->resource_id . '/?' . implode('&', $query_arr);
        } else {
            return $this->resource . '/?' . implode('&', $query_arr);
        }
    }
}